package com.yykj.hospital.server.handler;

import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;
import com.yykj.common.util.IDCardUtil;
import com.yykj.common.util.LocalStringUtil;
import com.yykj.common.util.MobileCheckUtil;
import com.yykj.hospital.server.dto.DoctorImportDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import javax.print.Doc;

@Slf4j
public class DoctorExcelHandler implements IExcelVerifyHandler<DoctorImportDTO> {
    private Integer rowNum = 1;


    @Override
    public ExcelVerifyHandlerResult verifyHandler(DoctorImportDTO doctorImportDTO) {
        StringBuilder sb = new StringBuilder();
        boolean flag = true;

        //工号	姓名	性别	科室编码	职称	联系方式
        if(StringUtils.isEmpty(doctorImportDTO.getJobNum())
        && StringUtils.isEmpty(doctorImportDTO.getDoctorName())
        && StringUtils.isEmpty(doctorImportDTO.getGender())
        && StringUtils.isEmpty(doctorImportDTO.getDepCode())
        && StringUtils.isEmpty(doctorImportDTO.getOfficer())
        && StringUtils.isEmpty(doctorImportDTO.getMobile())
        && StringUtils.isEmpty(doctorImportDTO.getInfo())
        && StringUtils.isEmpty(doctorImportDTO.getSkill())){
            doctorImportDTO.setSkip(Boolean.TRUE);
        }

        if(doctorImportDTO.getSkip()){
            return new ExcelVerifyHandlerResult(true, null);
        }

        doctorImportDTO.setMobile(StringUtils.trimAllWhitespace(doctorImportDTO.getMobile()));
        if(!MobileCheckUtil.checkMobile(doctorImportDTO.getMobile())){
            flag = false;
            sb.append("手机号不正确 ");
        }
        if (StringUtils.isEmpty(doctorImportDTO.getJobNum())) {
            flag = false;
            sb.append("工号不能为空 ");
        }
        if (StringUtils.isEmpty(doctorImportDTO.getDoctorName())) {
            flag = false;
            sb.append("姓名不能为空 ");
        }
        if(!StringUtils.isEmpty(doctorImportDTO.getIdCardNo())){
            if(!IDCardUtil.isIDCard(doctorImportDTO.getIdCardNo())){
                flag = false;
                sb.append("身份证号不正确");
            }
        }

        doctorImportDTO.setRowNum(rowNum++);
        doctorImportDTO.setErrorMsg(sb.toString());
        if (flag) {
            return new ExcelVerifyHandlerResult(true, null);
        } else {
            return new ExcelVerifyHandlerResult(false, sb.toString());
        }

    }
}
